package com.cj.springmvc.test.bo.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.cj.springmvc.core.dao.rdb.hibernate.HibernateDao;
import com.cj.springmvc.core.dao.rdb.query.parameter.Param;
import com.cj.springmvc.test.bo.PersonsBo;
import com.cj.springmvc.test.dto.PersonsDto;
import com.cj.springmvc.test.entity.Persons;
/**
 * since Hibernate 3.0 ,Hibernate provides the contextual Sessions ,which  
 * Hibernate itself manages one Session per transaction.That means there is 
 * no need for us to use HibernateTemplate to manage the sessions any longer.
 * In another word ,our DAO class is now decoupled to the Spring API.
 * @author cj
 * @since 2012-12-01
 * */
@Repository("personsBo")
public class PersonsImpl implements PersonsBo {
	private Logger log = Logger.getLogger(PersonsImpl.class);
	
	@Resource(name = "hibernateDao")
	private HibernateDao hibernateDao;
	
	@Transactional
	@Override
	public boolean doCheckLoginIsValid(PersonsDto dto) {
		// whether the username is valid 
//		String hql = "SELECT password FROM Persons where username = :username";
		String hql = "SELECT count(password) FROM Persons WHERE username =:username";
//		Query query = getSession().createQuery(hql);
//		query.setParameter("username", dto.getUsername());
		//This is not a good way to check whether the password is valid !! 
		/*@SuppressWarnings("unchecked")
		List<String> passwords = query.list();*/
//		long passwordLength = (Long) query.uniqueResult();
		long passwordLength = hibernateDao.findUniqueCommonObjects(hql, 
				Param.getVarcharParam("username", dto.getUsername()));
		log.info(passwordLength);
		if(passwordLength == 0) {
			return false;
		} else {
			if(passwordLength == 1)
				return true;
		}
		
		return false;
	}
	
	@Transactional
	@Override
	public void doSaveNewmember(PersonsDto dto) {
		log.info(String.format("于%s新增一名用户！", new Date()));
		Persons person = new Persons();
		person.setUsername(dto.getUsername());
		person.setPassword(dto.getPassword());
		person.setEmail(dto.getEmail());
		person.setAuthority("ROLE_USER");
		hibernateDao.save(person);
	}
	
	@Transactional
	@Override
	public boolean doCheckIsUsernameAvailable(String username) {
		// Bad way!!
//		String hql = "SELECT id FROM Persons WHERE username = :username";
		String hql = "SELECT count(id) FROM Persons WHERE username =:username";
		/*Query query = getSession().createQuery(hql);
		query.setParameter("username", username);*/
		long idLength = hibernateDao.findUniqueCommonObjects(hql, 
				Param.getVarcharParam("username", username));
		/*@SuppressWarnings("unchecked")
		List<Long> ids = query.list();*/
//		long idLength = (Long) query.uniqueResult();
		if(idLength == 0){
			return true;
		} else {
			return false;
		}
	}

	@Override
	@Transactional
	public void doDeletePerson(Long id) {
		hibernateDao.delete(id, Persons.class);
	}
	
	
}
